package com.whz.generic.message;

import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.context.support.ResourceBundleMessageSource;

import java.nio.charset.StandardCharsets;

/**
 * 默认从generic-message.properties查询话术
 *
 * @author whz
 */
public class GenericMessageAccessor extends ResourceBundleMessageSource {

    public GenericMessageAccessor() {
        this("generic-message");
    }

    public GenericMessageAccessor(String basename) {
        setBasename(basename);
        setDefaultEncoding(StandardCharsets.UTF_8.toString());
    }

    public static MessageSourceAccessor getAccessor(String baseBean) {
        return new MessageSourceAccessor(new GenericMessageAccessor(baseBean));
    }


    public static MessageSourceAccessor getAccessor() {
        return new MessageSourceAccessor(new GenericMessageAccessor());
    }

    /**
     * 带参数的话术
     *
     * @param code 话术key
     * @param args 话术参数
     * @return 补充参数的话术
     */
    public static String getMessage(final String code, final Object... args) {
        return getAccessor().getMessage(code, args);
    }

    /**
     * 指定了话术文件名
     *
     * @param baseBean 话术文件名
     * @param code     关键词
     * @param args     填充参数
     * @return 话术
     */
    public static String getMessage(final String baseBean, final String code, final Object... args) {
        return getAccessor(baseBean).getMessage(code, args);
    }

    /**
     * 话术
     *
     * @param code 话术key
     * @return 话术
     */
    public static String getMessage(final String code) {
        return getAccessor().getMessage(code);
    }

    public static String getMessage(final String baseBean, final String code) {
        return getAccessor(baseBean).getMessage(code);
    }
}